home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------- main.c ----------------------------------*/
- /* Copyright 1989 Brown University -- Jeffrey Vogel */
- /*----------------------------------------------------------------------------*/
-
- #define QD_BOSS
-
-
- /*--------------------------------- Includes ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- #include "qd_local.h"
- #include "patterns.bm"
- #include <X11/Xresource.h>
- #include <stdio.h>
-
-
- /*--------------------------------- Statics ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- static XrmDatabase QDDB;
-
-
-
- /*----------------------------- GetSstringFromDb -----------------------------*/
- /*----------------------------------------------------------------------------*/
-
- static
- char *
- GetStringFromDb(name)
- char *name;
- {
- char *str_type[20];
- XrmValue value;
- char *newval;
-
- XrmGetResource(QDDB, name, "Quickdraw.Attribute",
- str_type, &value);
- newval = (char *) malloc(strlen(value.addr) + 1);
- strcpy(newval, value.addr);
- return newval;
- }
-
-
-
- /*---------------------------------- InitDb ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- static
- void
- InitDb()
- {
- XrmDatabase user;
-
- XrmInitialize();
- QDDB = XrmGetFileDatabase(X_DEFAULTS);
- if (QDdisplay->xdefaults) {
- user = XrmGetStringDatabase(QDdisplay->xdefaults);
- XrmMergeDatabases(user, &QDDB);
- }
- F1 = GetStringFromDb("quickdraw.font1");
- F2 = GetStringFromDb("quickdraw.font2");
- F3 = GetStringFromDb("quickdraw.font3");
- F4 = GetStringFromDb("quickdraw.font4");
- }
-
-
- /*-------------------------------- InitColor ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- static
- void
- InitColor()
- {
- XColor exact, color;
-
- /*** get the cmap ***/
- QDcolormap = DefaultColormap(QDdisplay, QDscreen);
-
- /*** get all colors 8 ***/
- XAllocNamedColor(QDdisplay, QDcolormap, "white",
- &exact, &color);
- QDcolor__white = color.pixel;
-
- XAllocNamedColor(QDdisplay, QDcolormap, "black",
- &exact, &color);
- QDcolor__black = color.pixel;
-
- XAllocNamedColor(QDdisplay, QDcolormap, "red",
- &exact, &color);
- QDcolor__red = color.pixel;
-
- XAllocNamedColor(QDdisplay, QDcolormap, "green",
- &exact, &color);
- QDcolor__green = color.pixel;
- XAllocNamedColor(QDdisplay, QDcolormap, "blue",
- &exact, &color);
- QDcolor__blue = color.pixel;
- XAllocNamedColor(QDdisplay, QDcolormap, "cyan",
- &exact, &color);
- QDcolor__cyan = color.pixel;
- XAllocNamedColor(QDdisplay, QDcolormap, "yellow",
- &exact, &color);
- QDcolor__yellow = color.pixel;
- XAllocNamedColor(QDdisplay, QDcolormap, "magenta",
- &exact, &color);
- QDcolor__magenta = color.pixel;
- }
-
-
-
-
- /*--------------------------------- QDerror ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- QDerror(str)
- char *str;
- {
- if (QDerrorHandler)
- (*QDerrorHandler)(str);
- else printf("%s\n", str);
- }
-
-
-
-
-
-
-
-
-
-
- /*--------------------------------- QDreset ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- QDreset()
- {
- if (QDrunning)
- QuitDraw();
- }
-
-
-
- /*--------------------------------- QDclose ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- QDclose()
- {
- register int i;
-
- QDerrorHandler = NULL;
-
- /*** kill the data structures ***/
- for (i = 0; i < QD__MAX_FONTS; i++)
- XFreeFont(QDdisplay, QDfontInfo[i]);
- XFreeGC(QDdisplay, QDgc);
- XFreeGC(QDdisplay, QDinvertgc);
- XFreeGC(QDdisplay, QDgridgc);
- XFreePixmap(QDdisplay, QDstipple__dkGray);
- XFreePixmap(QDdisplay, QDstipple__ltGray);
- XFreePixmap(QDdisplay, QDstipple__gray);
- XFreePixmap(QDdisplay, QDstipple__white);
- XFreePixmap(QDdisplay, QDstipple__black);
-
- /*** close display ***/
- XCloseDisplay(QDdisplay);
-
- QDautoDisplay = FALSE;
- QDdisplay = NULL;
- }
-
-
- /*---------------------------------- QDopen ----------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- QDopen(display, errorHandler)
- char *display;
- FunctionPtr errorHandler;
- {
- int depth;
- Status retval;
- XColor color;
- Font f;
-
- /*** open server ***/
- QDdisplay = XOpenDisplay(display);
- if (QDdisplay == NULL) {
- QDerror("ERROR QDopen: Unable to open display.");
- return;
- }
-
-
-
-
-
- /*** set variables ***/
- QDerrorHandler = errorHandler;
- XSynchronize(QDdisplay, FALSE);
- QDscreen = DefaultScreen(QDdisplay);
- QDroot = RootWindow(QDdisplay, QDscreen);
-
- /*** get QDDB ***/
- InitDb();
-
- /*** init seed ***/
- srandom(1);
-
- /*** load fonts ***/
- f = QDfonts[FONT__SMALL] = XLoadFont(QDdisplay, F1);
- if (!f) {
- QDerror("ERROR QDopen: Unable to open fonts.");
- return;
- }
- f = QDfonts[FONT__MEDIUM] = XLoadFont(QDdisplay, F2);
- if (!f) {
- QDerror("ERROR QDopen: Unable to open fonts.");
- return;
- }
- f = QDfonts[FONT__LARGE] = XLoadFont(QDdisplay, F3);
- if (!f) {
- QDerror("ERROR QDopen: Unable to open fonts.");
- return;
- }
- f = QDfonts[FONT__LARGEST] = XLoadFont(QDdisplay, F4);
- if (!f) {
- QDerror("ERROR QDopen: Unable to open fonts.");
- return;
- }
- QDfontInfo[FONT__SMALL] = XQueryFont(QDdisplay, QDfonts[FONT__SMALL]);
- QDfontInfo[FONT__MEDIUM] = XQueryFont(QDdisplay, QDfonts[FONT__MEDIUM]);
- QDfontInfo[FONT__LARGE] = XQueryFont(QDdisplay, QDfonts[FONT__LARGE]);
- QDfontInfo[FONT__LARGEST] = XQueryFont(QDdisplay, QDfonts[FONT__LARGEST]);
-
-
- /*** create the GC ***/
- QDgcValues.function = FUNCTION__DEFAULT;
- QDgcValues.line_width = LINE_WIDTH__DEFAULT;
- QDgcValues.line_style = LINE_STYLE__DEFAULT;
- QDgcValues.font = QDfonts[FONT__DEFAULT];
- QDgcValues.foreground = BlackPixel(QDdisplay, QDscreen);
- QDgcValues.background = WhitePixel(QDdisplay, QDscreen);
- QDgcValues.graphics_exposures = FALSE;
- QDgc = XCreateGC (QDdisplay, QDroot,
- GCFont|GCFunction|GCLineWidth|GCLineStyle|
- GCForeground|GCBackground|
- GCGraphicsExposures|GCFont,
- &QDgcValues);
-
-
- /*** init color stuff ***/
- depth = DefaultDepth(QDdisplay, QDscreen);
- QDcolor = (depth > 1);
- if (QDcolor) {
- InitColor();
- }
-
-
-
-
-
-
- /*** create other gcs ***/
- QDinvertgc = XCreateGC (QDdisplay, QDroot, NULL, NULL);
- XSetFunction(QDdisplay, QDinvertgc, GXxor);
- XSetForeground(QDdisplay, QDinvertgc, BlackPixel(QDdisplay, QDscreen));
- XSetBackground(QDdisplay, QDinvertgc, WhitePixel(QDdisplay, QDscreen));
- XSetFillStyle(QDdisplay, QDinvertgc, FillSolid);
-
- /*** grid gc ***/
- QDgridgc = XCreateGC (QDdisplay, QDroot,
- GCGraphicsExposures,
- &QDgcValues);
- XSetForeground(QDdisplay, QDgridgc, BlackPixel(QDdisplay, QDscreen));
- XSetBackground(QDdisplay, QDgridgc, WhitePixel(QDdisplay, QDscreen));
- XSetFillStyle(QDdisplay, QDgridgc, FillSolid);
- XSetLineAttributes(QDdisplay, QDgridgc, 1,
- LineOnOffDash, CapNotLast, JoinMiter);
-
- /*** add patterns ***/
- QDstipple__dkGray =
- XCreateBitmapFromData(QDdisplay, QDroot, bitpat_1, 8, 8);
- QDstipple__ltGray =
- XCreateBitmapFromData(QDdisplay, QDroot, bitpat_23, 8, 8);
- QDstipple__gray =
- XCreateBitmapFromData(QDdisplay, QDroot, bitpat_3, 8, 8);
- QDstipple__white =
- XCreateBitmapFromData(QDdisplay, QDroot, bitpat_38, 8, 8);
- QDstipple__black =
- XCreateBitmapFromData(QDdisplay, QDroot, bitpat_0, 8, 8);
- }
-
-
-
-
-
- /*----------------------------- QDallowKeypress ------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- QDallowKeypress()
- {
- if (!QDrunning)
- return;
- XSelectInput(QDdisplay, QDwindow, StructureNotifyMask|KeyPressMask);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-